package com.mrd.gtimp.sup.action;

import com.mrd.constant.MsgConstants;
import com.mrd.exception.QueryException;
import com.mrd.framework.config.web.json.Result;
import com.mrd.framework.ui.Jqgrid;
import com.mrd.framework.ui.filter.SearchRule;
import com.mrd.framework.ui.filter.SearchRuleOption;
import com.mrd.framework.utils.HttpUtils;
import com.mrd.gtimp.sup.entity.SupCustomerRole;
import com.mrd.gtimp.sup.service.SupCustomerRoleBiz;
import com.mrd.util.LoginUtil;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;


/**
 * <p> application name: gtimp <p> copyright: Copyright@2012 明睿达科技有限公司
 */

/**
 * @author luob
 */

@Controller
@RequestMapping("/sup")
public class SupCustomerRoleAction {
    private static final long serialVersionUID = 1L;
    @Resource
    private SupCustomerRoleBiz supCustomerRoleBiz;

    /**
     * 默认访问入口
     */
    @RequestMapping("/supCustomerRole-main")
    public String main() {
        return "/gtimp/sup/supCustomerRoleList";
    }

    /**
     * 默认新增修改入口
     */
    @RequestMapping("/supCustomerRole-manage")
    public String manage(String id, HashMap map) {
        map.put("id", id);
        return "/gtimp/sup/supCustomerRoleManage";
    }

    /**
     * 新增或修改信息
     */
    //// TODO: 2016/8/15  Permissions
//    @Permissions(permissionsId = RoleConstants.SUP_CUSTOMER_ROLE_SAVE)
    @RequestMapping("/supCustomerRole-saveOrUpdate")
    @ResponseBody
    public Result saveOrUpdate(SupCustomerRole supCustomerRole) {
        Result msg;
        // if(checkRight(RoleConstants.SUP_CUSTOMER_SAVE)) {
        try {
            //// TODO: 2016/8/25  下拉框
            if (StringUtils.isEmpty(supCustomerRole.getID())) {  // 新增操作
                supCustomerRole.setCOM_ID(LoginUtil.getLoginUser().getCompany().getId());
                supCustomerRole.setCREATOR(LoginUtil.getLoginUser().getAccount());
                supCustomerRole.setCTIME(new Date());
                supCustomerRole.setID(null);
            } else {  // 修改操作
                supCustomerRole.setMODIFIER(LoginUtil.getLoginUser().getAccount());
                supCustomerRole.setMTIME(new Date());
            }
            supCustomerRoleBiz.saveOrupdateEntity(supCustomerRole);
            msg = Result.newSuccessInstance(MsgConstants.SAVE_SUECCES);
        } catch (Exception e) {
            e.printStackTrace();
            msg = Result.newFailInstance(MsgConstants.SAVE_FAIL + e.getMessage());
        }
        return msg;
    }

    /**
     * 编辑信息
     */
    @RequestMapping("/supCustomerRole-find")
    @ResponseBody
    public SupCustomerRole find(SupCustomerRole supCustomerRole) {
        if (null != supCustomerRole && StringUtils.isNotEmpty(supCustomerRole.getID())) {
            try {
                supCustomerRole = supCustomerRoleBiz.findEntityById(supCustomerRole.getID());
                return supCustomerRole;
            } catch (QueryException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 删除信息
     */
    //// TODO: 2016/8/15   @Permissions
//    @Permissions(permissionsId = RoleConstants.SUP_CUSTOMER_ROLE_DEL)
    @RequestMapping("/supCustomerRole-del")
    @ResponseBody
    public Result del() {
        Result msg;
        // if(checkRight(RoleConstants.SUP_CUSTOMER_DEL)) {
        try {
            String ids = HttpUtils.getRequest().getParameter("id");
            String[] idArr = ids.split(",");
            List<SupCustomerRole> del = new ArrayList<>();
            for (String string : idArr) {
                SupCustomerRole supCustomerRole = new SupCustomerRole();
                supCustomerRole.setID(string);
                del.add(supCustomerRole);
            }
            supCustomerRoleBiz.delete(del);
            msg = Result.newSuccessInstance(MsgConstants.DEL_SUCCES);
        } catch (Exception e) {
            e.printStackTrace();
            msg = Result.newFailInstance(MsgConstants.DEL_FAIL + e.getMessage());
        }
        return msg;
    }

    /**
     * 查询
     */
    @RequestMapping("/supCustomerRole-findList")
    @ResponseBody
    public Page<SupCustomerRole> findList(Jqgrid jqgrid) {
        String comId = LoginUtil.getLoginUser().getCompany().getId();// get from user
        try {
            SearchRule rule = new SearchRule("COM_ID", SearchRuleOption.eq, comId);
            jqgrid.addRule(rule);
//            page = initPage(supCustomerRoleBiz.findEntityCount(filters));
//            Order order = new Order();
//            if (null != sidx && !"".equals(sidx)) {
//                order.add(sidx, sord);
//            }
//            supCustomerRoles = supCustomerRoleBiz
//                    .findEntityByFilter(page.getCurrentPageStartNo(), page.getPageSize(), filters, order);
            Page<SupCustomerRole> supCustomerRoles = supCustomerRoleBiz.findByJqgrid(jqgrid);
            return supCustomerRoles;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    //// TODO: 2016/8/15   @Permissions
//    @Permissions(permissionsId = RoleConstants.SUP_CUSTOMER_ROLE_SAVE)
    @RequestMapping("/supCustomerRole-setDefault")
    @ResponseBody
    public Result setDefault() {
        Result msg;
        // if(checkRight(RoleConstants.SUP_CUSTOMER_SAVE)) {
        try {
            String id = HttpUtils.getRequest().getParameter("id");
            supCustomerRoleBiz
                    .updateByHql("update com.mrd.gtimp.sup.entity.SupCustomerRole set IS_DEFAULT='否' where COM_ID='"
                            + LoginUtil.getLoginUser().getCompany().getId() + "'");
            supCustomerRoleBiz
                    .updateByHql("update com.mrd.gtimp.sup.entity.SupCustomerRole set IS_DEFAULT='是' where COM_ID='"
                            + LoginUtil.getLoginUser().getCompany().getId() + "' and ID='" + id + "'");
            msg = Result.newSuccessInstance(MsgConstants.OPERATION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            msg = Result.newFailInstance(MsgConstants.OPERATION_FAIL + e.getMessage());
        }
        return msg;
    }

    @Resource
    public void setSupCustomerRoleBiz(SupCustomerRoleBiz supCustomerRoleBiz) {
        this.supCustomerRoleBiz = supCustomerRoleBiz;
    }

}
